package com.wnnight.wys.mapper.config;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wnnight.wys.entity.config.ChargeAccount;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @ClassName: ChargeAccountMapper
 * @Description: //收费账号mapper
 * @author: LZJ
 * @date: 2022/8/27-18:43
 * @version: v1.0
 */
public interface ChargeAccountMapper extends BaseMapper<ChargeAccount> {
    @Insert("insert into wys_charge_account(account,update_time,status,pay_id)values(#{account},#{updateTime},#{status},#{payMentMethod.id})")
    void add(ChargeAccount chargeAccount);

    @UpdateProvider(type =sqlProvider.class, method = "update")
    void update(ChargeAccount chargeAccount);

    //收费配置mapper动态sql
    class sqlProvider {
        //修改
        public static String update(ChargeAccount chargeAccount) {
            String sql = new SQL() {
                {
                    UPDATE("wys_charge_account");
                    if (chargeAccount.getAccount() != null) {
                        SET("account=#{account}");
                    }
                    if (chargeAccount.getUpdateTime() != null) {
                        SET("update_time=#{updateTime}");
                    }
                    if (chargeAccount.getStatus() != null) {
                        SET("status=#{status}");
                    }
                    if (chargeAccount.getPayMentMethod() != null) {
                        SET("pay_id=#{payMentMethod.id}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
            return sql;
        }
    }
    @Select("select * from wys_charge_account wcc,wys_payment_method wpm where wcc.pay_id=wpm.id")
    @Results(
            {
                    @Result(column = "id", property = "id", id = true),
                    @Result(column = "account", property = "account"),
                    @Result(column = "update_time", property = "updateTime"),
                    @Result(column = "status", property = "status"),
                    @Result(column = "method", property = "payMentMethod.method"),

            })
    List<ChargeAccount> getAll();

    @Select("select * from wys_charge_account wcc,wys_payment_method wpm where wcc.pay_id=wpm.id and wcc.id=#{id}")
    @Results(
            {
                    @Result(column = "id", property = "id", id = true),
                    @Result(column = "account", property = "account"),
                    @Result(column = "update_time", property = "updateTime"),
                    @Result(column = "status", property = "status"),
                    @Result(column = "pay_id", property = "payMentMethod.id"),
                    @Result(column = "method", property = "payMentMethod.method"),

            })
    ChargeAccount getById(int id);
}